function openMapByDefault(latitude, longitude, name) {
	uni.openLocation({
		latitude: latitude,
		longitude: longitude,
		name: name,
		address:"",
		fail: (err) => {
			console.log(err)
		},
	})
}
function openMapByAndroid(latitude, longitude, name) {
	let url = ''; // 回调地址
	let downloadUrl = ''; // 回调地址
	uni.showActionSheet({
		itemList:["百度地图","高德地图","腾讯地图"],
		success: (res) => {
			console.log(res)
			if(res.tapIndex == 0){
				url = "bdapp://map/direction?destination=name:"+name+"|latlng:"+latitude+","+longitude+"&coord_type=gcj02&src=hxbank";
				downloadUrl = "http://map.baidu.com/zt/qudao/newfengchao/1012337a/html/slide.html"
				openURL(url,downloadUrl)
			}else if(res.tapIndex == 1){
				url = "amapuri://route/plan/?sourceApplication=hxbank&dlat="+latitude+"&dlon="+longitude+"&dname="+name+"&dev=1";
				downloadUrl = "http://wap.amap.com/"
				openURL(url,downloadUrl)
			}else{
				url = "qqmap://map/routeplan?type=drive&to="+name+"&tocoord="+latitude+","+longitude+""
				openURLTencent(url,latitude, longitude, name)
			}
		}
	})
}
function openMapByIos(latitude, longitude, name) {
	let url = ''; // 回调地址
	let downloadUrl = ''; // 回调地址
	uni.showActionSheet({
		itemList:["百度地图","高德地图","腾讯地图"],
		success: (res) => {
			console.log(res)
			if(res.tapIndex == 0){
				url = "baidumap://map/direction?destination=name:"+name+"|latlng:"+latitude+","+longitude+"&coord_type=gcj02&src=hxban";
				downloadUrl = "http://map.baidu.com/zt/qudao/newfengchao/1012337a/html/slide.html";
				openURL(url,downloadUrl)
			}else if(res.tapIndex == 1){
				url = "iosamap://route/plan/?sourceApplication=hxbank&dlat="+latitude+"&dlon="+longitude+"&dname="+name+"&dev=1";
				downloadUrl = "http://wap.amap.com/"
				openURL(url,downloadUrl)
			}else{
				url = "qqmap://map/routeplan?type=drive&to="+name+"&tocoord="+latitude+","+longitude+""
				openURLTencent(url,latitude, longitude, name)
			}
		}
	})
}
function openURL(url,downLoadUrl) {
	window.location.href=url;
	var startTime = Date.now();
	var count = 0;
	var endTime = 0;
	var t = setInterval(function () {
	  count += 1;
	  endTime = Date.now() - startTime;
	  if (endTime > 800) {
	    clearInterval(t);
	  }
	  if (count < 40){
		  return false;
	  } 
	  if (!(document.hidden || document.webkitHidden)) {
	    window.location.href = downLoadUrl;
	  }
	}, 20);
}
function openURLTencent(url,latitude, longitude, name) {
	window.location.href=url;
	var startTime = Date.now();
	var count = 0;
	var endTime = 0;
	var t = setInterval(function () {
	  count += 1;
	  endTime = Date.now() - startTime;
	  if (endTime > 800) {
	    clearInterval(t);
	  }
	  if (count < 40){
		  return false;
	  } 
	  if (!(document.hidden || document.webkitHidden)) {
	   openMapByDefault(latitude, longitude, name)
	  }
	}, 20);
}
export default {
	/* 打开地图 */
	openMap(latitude, longitude, name) {
		// #ifdef MP-WEIXIN
			wx.openLocation({
				latitude: latitude,
				longitude: longitude,
				name: name,
				fail: (err) => {
					console.log(err)
				},
			})
		// #endif
		// #ifndef MP
			switch(uni.getSystemInfoSync().platform){
				case 'android':
					console.log('运行Android上')
					openMapByAndroid(latitude, longitude, name)
					break;
				case 'ios':
					console.log('运行iOS上')
					openMapByIos(latitude, longitude, name,)
					break;
				default:
					openMapByDefault(latitude, longitude, name)
					console.log('运行在开发者工具上')	
					break;
			}
			
		// #endif
		
	}
}